package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.EquipmentUnitType;
import com.yqg.protection.service.IEquipmentUnitTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * EquipmentUnitTypeController 前端控制器
 *
 * @author zxf
 * @since 2024-09-18
 */
@RestController
@RequestMapping("/equipment-unit-type")
public class EquipmentUnitTypeController {

    @Resource
    private IEquipmentUnitTypeService equipmentUnitTypeService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改设备部件类型")
    public Result save(@RequestBody EquipmentUnitType equipmentUnitType) {
        equipmentUnitType.setIsDelete(0);
        return Result.success(equipmentUnitTypeService.saveOrUpdate(equipmentUnitType));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除设备部件类型")
    public Result delete(@PathVariable Integer id) {
        EquipmentUnitType equipmentUnitType = equipmentUnitTypeService.getById(id);
        equipmentUnitType.setIsDelete(1);
        return Result.success(equipmentUnitTypeService.saveOrUpdate(equipmentUnitType));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<EquipmentUnitType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<EquipmentUnitType> list = equipmentUnitTypeService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(equipmentUnitTypeService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name) {
        QueryWrapper<EquipmentUnitType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        queryWrapper.like("type_name", name);
        return Result.success(equipmentUnitTypeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

